package com.zdsoft.blp.zongbuju.shouye.bianmin.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.bianmin.adapter.BMbsznAdapter;
import com.zdsoft.blp.zongbuju.shouye.bianmin.entity.SheQu;
import com.zdsoft.blp.zongbuju.shouye.bianmin.entity.ZhiNan;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class BMbsznActivity extends BaseActivity implements XListView.IXListViewListener,NetCallback{
    private TextView tv_title;
    private XListView xlv_bszn;
    private List<ZhiNan>list;
    private List<ZhiNan>mlist;
    private ZhiNan zhiNan;
    private int pageNo=1;
    private BMbsznAdapter adapter;
    @Override
    protected void init() {
        setContentView(R.layout.bianmin_bszn);
        xlv_bszn=find(R.id.xlv_bszn);
        tv_title = find(R.id.tv_title);
        tv_title.setText("办事指南");
        mlist=new ArrayList<>();
        xlv_bszn.setXListViewListener(this);
        xlv_bszn.setPullRefreshEnable(true);//设置可以刷新
        xlv_bszn.setPullLoadEnable(true);//设置可以加载更多
        showMoreData();


        xlv_bszn.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(BMbsznActivity.this,BMxqActivity.class);
                Bundle extra = new Bundle();
                ZhiNan zhiNan = mlist.get(position-1);
                extra.putSerializable("zhinan", zhiNan);
                intent.putExtra("id",mlist.get(position-1).getId());
                intent.putExtras(extra);
                intent.putExtra("title","zhinan");
                startActivity(intent);
            }
        });
    }

    private void showMoreData() {
        Net net=new Net();
        net.url= API.GENERALINFO_URL;
        net.nameSpace=API.GENERALINFO_NAMESPACE;
        net.methodName=API.GENERALINFOLIST ;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Map<String,Object> map=new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize",7);
        map.put("columnCode","zcfg");
        map.put("communityCode",communityCode);
        map.put("condition","");
        map.put("curTime","");
        net.parms=map;
        getServer(net,this);
    }

    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        showMoreData();
    }
    @Override
    public void onLoadMore() {
        pageNo++;
        showMoreData();
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag){
            if (pageNo==1){
                mlist=jiexi(res);
                adapter=new BMbsznAdapter(this,mlist);
                xlv_bszn.setAdapter(adapter);
                xlv_bszn.stopRefresh();
            }else {
                list=jiexi(res);
                for (ZhiNan zhiNans:list){
                    mlist.add(zhiNans);
                }
                adapter.setListData(mlist);
                xlv_bszn.stopLoadMore();
            }
        }

    }

    private List<ZhiNan> jiexi(String res) {
        list=new ArrayList<>();
        try {
            JSONObject object=new JSONObject(res);
            String total=object.getString("total");
            String pageNo=object.getString("pageNo");
            String pageSize=object.getString("pageSize");
            JSONArray array=object.getJSONArray("rows");
            for(int i=0;i<array.length();i++){
                JSONObject object2=array.getJSONObject(i);
                String id=object2.getString("id");
                String picPath=object2.getString("picPath");
                String content=object2.getString("content");
                String title=object2.getString("title");
                String releaseDate=object2.getString("releaseDate");
                zhiNan=new ZhiNan(id,title);
                list.add(zhiNan);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
